﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using Keepo.Helpers;
using Keepo.Models;
using System.Drawing.Drawing2D;
using AlbumDesigner.Helpers;

namespace AlbumDesigner.Controllers
{
    public class ImagesController : Controller
    {
        AlbumContext db = new AlbumContext();

        public ActionResult ResizeImage(int ImageId, int Width)
        {
            //get the image url of this image
            var image = db.DesignImages.FirstOrDefault(m => m.DesignImageId == ImageId);
            var imageUrl = image.URL;

            var contentType = FilesHelper.GetContentType(imageUrl);

            Image img = Image.FromFile(Server.MapPath("~/" + imageUrl));

            int imageWidth = img.Width;
            int imageHeight = img.Height;
            if (Width > imageWidth)
            {
                return File("~/" + imageUrl, contentType);
            }

            var sizingDifference = (double)Width / (double)imageWidth;

            var Height = (int)(imageHeight * sizingDifference);

            Bitmap dstImage = new Bitmap(Width, Height);

            using (Graphics g = Graphics.FromImage((Image)dstImage))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, 0, 0, Width, Height);
                img.Dispose();
            }

            
            EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)96 /*quality*/);
            ImageCodecInfo imageCodec = ImageHelper.GetEncoderInfo(contentType);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            MemoryStream ms = new MemoryStream();
            dstImage.Save(ms, imageCodec, encoderParams);
            dstImage.Dispose();

            // make byte array the same size as the image
            byte[] imageContent = new Byte[ms.Length];

            // rewind the memory stream
            ms.Position = 0;

            // load the byte array with the image
            ms.Read(imageContent, 0, (int)ms.Length);

            return File(imageContent, contentType);
        }

        public ActionResult getPoint()
        {
            Point[] points = { new Point(3, 4), new Point(3, 4) };

            return Json(points, JsonRequestBehavior.AllowGet);
        }


        static PointF scalePoint(PointF p, float s)
        {
            return new PointF(p.X * s, p.Y * s);
        }
        
        // Give the width and hight in 72 dpi
        // Transformation matrix of inner place
        // and new factor
        public ActionResult CropImage(int ImageId, 
            int width = 0, int height = 0, 
            float a = 1, float b = 0, float c = 0, float d = 1,
            float tx = 0, float ty = 0,
            float factor = 0
            )
        {

            Matrix matrix = new Matrix(a, b, c, d, tx, ty);
            
            if (factor == 0)
            {
                factor = 300f / 72f;
            }

            matrix.Scale(factor, factor);
            width = (int) (factor * width);
            height = (int) (factor * height);

            //get the image url of this image
            var image = db.DesignImages.FirstOrDefault(m => m.DesignImageId == ImageId);
            var points = new PointF[] { new PointF(0,0),
                new PointF(image.ImageWidth, 0),
                new PointF(0, image.ImageHeight) };

            matrix.TransformPoints(points);
			

            var imageUrl = image.URL;
            var contentType = FilesHelper.GetContentType(imageUrl);

            Image img = Image.FromFile(Server.MapPath("~/" + imageUrl));

            

            Bitmap dstImage = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage((Image)dstImage))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(img, points);
                img.Dispose();
            }


            EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)96 /*quality*/);
            ImageCodecInfo imageCodec = ImageHelper.GetEncoderInfo(contentType);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            MemoryStream ms = new MemoryStream();
            dstImage.Save(ms, imageCodec, encoderParams);
            dstImage.Dispose();

            // make byte array the same size as the image
            byte[] imageContent = new Byte[ms.Length];

            // rewind the memory stream
            ms.Position = 0;

            // load the byte array with the image
            ms.Read(imageContent, 0, (int)ms.Length);

            return File(imageContent, contentType);
        }
    }
}
